package com.tcs.test.spring;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Maps;
import com.tcs.test.BaseJunit4Test;
import com.tcs.util.bean.BeanUtils;
import com.tcs.util.spring.SpringConfigTool;
import com.tcs.util.spring.SpringIocUtils;
import com.tcs.util.spring.SpringSpoceEnum;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

public class SpringTest1 extends BaseJunit4Test {

	private final Logger logger = LoggerFactory.getLogger(SpringTest1.class);

	public void test1() {
		TestBean testBean = new TestBean();
		testBean.setSs(111);
		Map<String, Object> paramsMap = null;
		try {
			paramsMap = BeanUtils.BeanToMap(testBean);
		} catch (NullPointerException e) {
			logger.error("" , e);
		} catch (IllegalArgumentException e) {
			logger.error("" , e);
		} catch (IllegalAccessException e) {
			logger.error("" , e);
		}
		try {
			
			SpringIocUtils.setRegisterBean(TestBean.class, paramsMap , SpringSpoceEnum.SINGLETON);
			TestBean testBean1 = (TestBean) SpringConfigTool.getBean(testBean.getClass().getName());
			TestBean testBean2 = (TestBean) SpringConfigTool.getBean(testBean.getClass().getName());
			logger.info(" testBean1 : {} , paramsMap : {}  ", testBean2 == testBean1 , paramsMap);
		} catch (Exception e) {
			logger.error("" , e);
		}
	}
	
	@Test
	public void test2() {
		Class<?> clazz = null;
		try {
			clazz = Class.forName("redis.clients.jedis.JedisCluster");
		} catch (ClassNotFoundException e1) {
			logger.error("" , e1);
		}
		Map<String,Object> paramsMap = Maps.newLinkedHashMap();
//		String [] fileds = new String[]{"jedisClusterNode", "connectionTimeout" , "soTimeout" , "maxAttempts" , "password" , "poolConfig"};
		Set<HostAndPort> set = new HashSet<HostAndPort>();
		set.add(new HostAndPort("" , 1111));
		
		paramsMap.put("jedisClusterNode", set);
		paramsMap.put("connectionTimeout", 20);
		paramsMap.put("soTimeout", 20);
		paramsMap.put("maxAttempts", 20);
		paramsMap.put("poolConfig", new GenericObjectPoolConfig());
		try {
			SpringIocUtils.setRegisterBean(clazz, null , paramsMap , SpringSpoceEnum.SINGLETON);
			JedisCluster testBean1 = (JedisCluster) SpringConfigTool.getBean(JedisCluster.class);
			JedisCluster testBean2 = (JedisCluster) SpringConfigTool.getBean(JedisCluster.class.getName());
		} catch (Exception e) {
			logger.error("" , e);
		} finally {
			// testBean2 == testBean1
			logger.info(" testBean1 : {} , paramsMap : {}  ", null , paramsMap);
		}
	}

	@Test
	public void test3() {
//		ITest itest = SpringConfigTool.getBean(ITest.class);
//		itest.print();
	}
}